package com.graduate.twentyone.controller;

import com.graduate.twentyone.common.R;
import com.graduate.twentyone.domain.Food;
import com.graduate.twentyone.domain.RestDate;
import com.graduate.twentyone.service.FoodService;
import com.graduate.twentyone.util.UploadConfig;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
public class FoodController {

    @Autowired
    private UploadConfig uc;

    @Autowired
    FoodService foodService;
    //    @RequiresUser
//    @RequiresAuthentication
//    @RequiresRoles("admin")

    @RequestMapping(value = "/food", method = RequestMethod.GET)
    @RequiresPermissions("food:select")
//    @GetMapping("/selectfood")
    public RestDate selectfood() {
        System.out.println(" food   select");
        System.out.println(SecurityUtils.getSubject().isAuthenticated());

        boolean permitted = SecurityUtils.getSubject().isPermitted("food:select");
        System.out.println("此处权限food:select为：" + permitted);

        List<Food> selectfood = foodService.selectfood();
        RestDate restDate = new RestDate(500, "发生未知错误", 500);
        if (selectfood.size() >= 0) {
            restDate = new RestDate(200, "food 查询成功", selectfood);
        }
        return restDate;
    }


//    前端现在不用这个了
//    顾客查询
    @RequestMapping(value = "/clientfood", method = RequestMethod.GET)
    @RequiresPermissions("food:select")
    public RestDate clientselectfood() {
        System.out.println(" food   select");
        System.out.println(SecurityUtils.getSubject().isAuthenticated());

        boolean permitted = SecurityUtils.getSubject().isPermitted("food:select");
        System.out.println("此处权限food:select为：" + permitted);

        List<Food> selectfood = foodService.clientselectfood();
        RestDate restDate = new RestDate(500, "发生未知错误", 500);
        if (selectfood.size() >= 0) {
            restDate = new RestDate(200, "food 查询成功", selectfood);
        }
        return restDate;
    }


        @RequiresPermissions("food:add")
    @RequestMapping(value = "/food", method = RequestMethod.POST)
    public RestDate addfood(Food food, MultipartFile Mf) {
        System.out.println("food add");
        food.setPicture(uc.upLoadFoodsImag(Mf));
        Integer addfood = foodService.addfood(food);
        RestDate restDate = new RestDate(500, "发生未知错误", "返回的数据");
        if (addfood == 1) {
            restDate = new RestDate(200, "成功", "返回的数据");
        }
        return restDate;
    }

    @RequiresPermissions("food:update")
    @RequestMapping(value = "/food", method = RequestMethod.PUT)
    public RestDate updatefood(Food food, MultipartFile Mf) {
        System.out.println("food update");
        Food food1 = new Food(food.getFid(), food.getName(), null, food.getPrice(), food.getMold(), food.getDes(), 1,food.getUndercarriage());
        if (Mf != null) {
            String s = uc.upLoadFoodsImag(Mf);
            food1.setPicture(s);
        }
        Integer updatefood = foodService.updatefood(food1);
        RestDate restDate = new RestDate(500, "发生未知错误", "返回的数据");
        if (updatefood == 1) {
            restDate = new RestDate(200, "成功", "返回的数据");
        }
        return restDate;
    }

    @RequiresPermissions("food:delete")
    @RequestMapping(value = "/food", method = RequestMethod.DELETE)
    public RestDate deletefood(@RequestBody Food food) {
        System.out.println("food delete");

        food.setStatus(0);
        Integer updatefood = foodService.updatefood(food);
        RestDate restDate = new RestDate(500, "发生未知错误", "返回的数据");
        if (updatefood == 1) {
            restDate = new RestDate(200, "成功", "返回的数据");
        }
        return restDate;
    }


}

